package neu.edu.vn.appmusic.utils;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import neu.edu.vn.appmusic.R;
import android.content.Context;
import android.util.Log;
import android.widget.ImageView;

import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.decode.BaseImageDecoder;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;

public class ImageUtils {
	private volatile static ImageUtils instance;
	DisplayImageOptions options;
	/** Returns singleton class instance */
	public static ImageUtils getInstance(Context context) {
		if (instance == null) {
			synchronized (ImageUtils.class) {
				if (instance == null) {
					instance = new ImageUtils(context);
				}
			}
		}
		return instance;
	}
	
	public ImageUtils(Context context){
		initLoad(context);
		
		options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.ic_stub)
		.showImageForEmptyUri(R.drawable.default_music)
		.showImageOnFail(R.drawable.ic_error)
		.cacheInMemory(true)
		.cacheOnDisk(true)
		.considerExifParams(true)
		.displayer(new RoundedBitmapDisplayer(10))
		.build();
	}
	
	
	public void loadImageToImageview(String urlString, ImageView imageView){
		ImageLoader imageLoader = ImageLoader.getInstance();
		imageLoader.displayImage(urlString, imageView, options);
	}
	
	public void initLoad(Context context){
		File cacheDir = StorageUtils.getCacheDirectory(context);
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
				.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
		        .memoryCacheSize(2 * 1024 * 1024)
		        .memoryCacheSizePercentage(13) // default
		        .diskCache(new UnlimitedDiscCache(cacheDir)) // default
		        .diskCacheSize(50 * 1024 * 1024)
		        .diskCacheFileCount(100)
		        .diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
				.build();
		
		ImageLoader.getInstance().init(config);
		DisplayImageOptions options = new DisplayImageOptions.Builder().build();
	}
}
